﻿using Basic.FormModel;
using Basic.FormModel.DB;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.QueryTable.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class PhysicalQueryTableDAL : BasicDAL<DBPhysicalQueryTable, long>, IPhysicalQueryTableDAL
    {
        public PhysicalQueryTableDAL ( IRepository<DBPhysicalQueryTable> basicDAL ) : base(basicDAL, "physical.query.table")
        {
        }
        public long Add ( PhyQueryTableAdd data, bool isMainTable )
        {
            DBPhysicalQueryTable add = new DBPhysicalQueryTable
            {
                CreateTime = DateTime.Now,
                FormId = data.FormId,
                Id = IdentityHelper.CreateId(),
                TableName = data.TableName,
                Show = data.Show,
                IsMainTable = isMainTable
            };
            DBPhysicalQueryColumn[] cols = isMainTable ? ConstDic.MainCol.ConvertAll(c => new DBPhysicalQueryColumn
            {
                ColLabel = c.Description,
                ColName = c.Name,
                DataType = c.DataType,
                IsNull = false,
                Sort = c.Sort,
                ColLen = c.Len,
                TableId = add.Id,
                Id = IdentityHelper.CreateId(),
                ShowMode = PhyColShowMode.Column,
                FormId = data.FormId,
                FuncType = FormColumnFuncType.无,
                IsHidden = true,
                IsQueryHide = true,
                IsSystem = true
            }) : ConstDic.SubCol.ConvertAll(c => new DBPhysicalQueryColumn
            {
                ColLabel = c.Description,
                ColName = c.Name,
                DataType = c.DataType,
                IsNull = false,
                Sort = c.Sort,
                ColLen = c.Len,
                TableId = add.Id,
                Id = IdentityHelper.CreateId(),
                ShowMode = PhyColShowMode.Column,
                FormId = data.FormId,
                FuncType = FormColumnFuncType.无,
                IsHidden = true,
                IsQueryHide = true,
                IsSystem = true
            });
            ISqlQueue<DBPhysicalQueryTable> queue = this._BasicDAL.BeginQueue();
            queue.Insert(add);
            queue.InsertBy<DBPhysicalQueryColumn>(cols);
            _ = queue.Submit();
            return add.Id;
        }
    }
}
